package com.zjz.servlet;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;

public class LoginServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        req.setCharacterEncoding("utf-8"); //接收乱码

        //java 获取到的数据
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String[] hobbys = req.getParameterValues("hobbys");
//控制台输出
        System.out.println("------------------------");
        System.out.println(username);
        System.out.println(password);
        System.out.println(Arrays.toString(hobbys));

        //通过请求转发
        System.out.println("路径：" + req.getContextPath());
        resp.setCharacterEncoding("utf-8"); //响应乱码
        resp.setContentType("text/html");
        System.out.println(password.isEmpty());
        if("123456".equals(password)&&!password.isEmpty()){
            resp.sendRedirect("/hello/success.jsp"); //正常响应重定向
        }else {

            resp.getWriter().println("密码错误");

        }

//        req.getRequestDispatcher("/success.jsp").forward(req,resp); //请求转发

//        ServletContext servletContext = this.getServletContext();
//        servletContext.getRequestDispatcher("/success.jsp").forward(req,resp);//context转发



    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
